/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg_customerManagement;

import pkg_group5.Database;
import java.sql.*;
/**
 *
 * @author mattcarr
 */
public class CustomerDatabaseFunctions extends Database{

    public CustomerDatabaseFunctions() {
        super("eBookSystem");
    }

    public int registerCustomer(String email, String first_name, String surname, String password) {
        
        String query = "INSERT INTO customers (email, first_name, surname, password) VALUES (?, ?, ?, ?)";

        try {
            PreparedStatement ps = conn.prepareStatement(query);
            ps.clearParameters();
            ps.setString(1, email);
            ps.setString(2, first_name);
            ps.setString(3, surname);
            ps.setString(4, password);

            int register = ps.executeUpdate();

            if (register == 1) {
                return 0;
            } else {
                return 1;
            }

        } catch (java.sql.SQLException e) {
            System.out.println(e);
            return -1;
        } catch (java.lang.NullPointerException e) {
            return -1;
        }

    }

    public int authenticateCustomer(String email, String pass) {

        String query = "SELECT customer_id from customers where email = ? AND password = ?";

        try {
            PreparedStatement ps = conn.prepareStatement(query);
            ps.clearParameters();
            ps.setString(1, email);
            ps.setString(2, pass);

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                return rs.getInt(1);
            }
        } catch (Exception e) {
            System.out.println(e);
            return -1;
        }

        return -1;
    }
    
    public String getCustomername(int id) {

        String query = "SELECT first_name, surname from customers where customer_id = " + id;

        try {
            PreparedStatement ps = conn.prepareStatement(query);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                String temp = rs.getString(1) + " " + rs.getString(2);
                return temp;
            }
        } catch (Exception e) {
            System.out.println(e);
            return "";
        }

        return "";
    }
}
